<?php

/***********************************************************************
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or    *
* (at your option) any later version.                                  *
*                                                                      *
* This program is distributed in the hope that it will be useful,      *
* but WITHOUT ANY WARRANTY; without even the implied warranty of       *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
* GNU General Public License for more details.                         *
*                                                                      *
* You should have received a copy of the GNU General Public License    *
* along with this program.  If not, see <http://www.gnu.org/licenses/> *
***********************************************************************/

// Image sizes
define ('GIS_LARGE', 'l');
define ('GIS_MEDIUM', 'm');
define ('GIS_ICON', 'i');
define ('GIS_ANY', '');

// Image types
define ('GIS_FACE', 'face');
define ('GIS_PHOTO', 'photo');
define ('GIS_CLIPART', 'clipart');
define ('GIS_LINEART', 'lineart');

function googleImageSearch ($query, $page = 1, $size = GIS_ANY, $type = GIS_ANY)
{

	$retVal = array();

	// Get the search results page
	$response = file_get_contents('http://images.google.com/images?hl=en&q=' . urlencode ($query) . '&imgsz=' . $size . '&imgtype=' . $type . '&start=' . (($page - 1) * 21));
	
	// Extract the image information. This is found inside of a javascript call to setResults
	preg_match('/dyn.setResults\(\[(.*?)\]\);/is', $response, $match);
	
	if (isset($match[1])) {
		
		// Grab all the arrays
		preg_match_all('/\[(.*?)\"\]/', $match[1], $m);
		
		foreach ($m[1] as $item) {
			// Explode on each paramter (comma delimeter)
			$item = urldecode(str_replace('\x', '%', $item));
			preg_match_all('/\"(.*?)\"/', $item, $params);
			
			// Check for more than one paramter. Not sure why, but there seem to be empty array sets between actual results
			if (count($params[1]) > 0) {
				
				$params = $params[1];
				
				// Important array indices
				// 0  - Link to Google image result. This is the page that displays when you click a result through normal image search
				// 3  - URL of source image
				// 4  - Width of the thumbnail image
				// 5  - Height of the thumbnail image
				// 6  - Title of the image
				// 9  - Width, height and size of the image (In the format of 'width &times; height - size')
				// 10 - Image type
				// 11 - Originating domain
				// 18 - URL of google's thumbnail
				
				preg_match('/([\d]+) &times; ([\d]+) - ([\d]+)/', $params[9], $dimensions);
				$t = null;
				$t->resultLink = 'http://images.google.com' . $params[0];
				$t->source = $params[3];
				$t->title = $params[6];
				$t->width = $dimensions[1];
				$t->height = $dimensions[2];
				$t->size = $dimensions[3];
				$t->type = $params[10];
				$t->domain = $params[11];
				$t->thumb = null;
				$t->thumb->src = $params[18];
				$t->thumb->width = $params[4];
				$t->thumb->height = $params[5];
				$retVal[] = $t;
				
			}
		}
		
	}
	
	return $retVal;
	
}